/*
 *  CECocos2dUtility.h
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#ifndef __CE_Cocos2dUtility_h__
#define __CE_Cocos2dUtility_h__

#include "cocos2d.h"


namespace cocosext {

// Cocos2dx
extern int getHigherTouchPriority(int touchPriority);

extern void pauseNodeAndChildren(cocos2d::CCNode* pNode);
extern void resumeNodeAndChildren(cocos2d::CCNode* pNode);

extern cocos2d::CCSprite* spritePair(const char* sppParent, const char* sppChild);
extern cocos2d::CCNode* spritePair(cocos2d::CCNode* sprParent, cocos2d::CCNode* sprChild);


// Alpha blending

#define CE_CCNODE_SET_BLENDING_NORMAL(node)		{ccBlendFunc blendFunc; blendFunc.src = GL_SRC_COLOR,	blendFunc.dst = GL_DST_COLOR;			node->setBlendFunc(blendFunc); }
#define CE_CCNODE_SET_BLENDING_SCREEN(node)		{ccBlendFunc blendFunc; blendFunc.src = GL_ONE,			blendFunc.dst = GL_ONE_MINUS_SRC_COLOR; node->setBlendFunc(blendFunc); }
#define CE_CCNODE_SET_BLENDING_ADD(node)		{ccBlendFunc blendFunc; blendFunc.src = GL_SRC_ALPHA,	blendFunc.dst = GL_ONE;					node->setBlendFunc(blendFunc); }
#define CE_CCNODE_SET_BLENDING_MULTIPLY(node)	{ccBlendFunc blendFunc; blendFunc.src = GL_ZERO,		blendFunc.dst = GL_SRC_COLOR;			node->setBlendFunc(blendFunc); }
#define CE_CCNODE_SET_BLENDING_BURN(node)		{ccBlendFunc blendFunc; blendFunc.src = GL_DST_COLOR,	blendFunc.dst = GL_SRC_COLOR;			node->setBlendFunc(blendFunc); }


// Wchar dictionary make

/**
 * Returns LID Library dictionary - the same as CCDictionary, but with CEStrings except CCStrings 
 */
extern cocos2d::CCDictionary<std::string, cocos2d::CCObject*> *cocosextDictionaryWithContentsOfFile(const char *pFileName);
extern cocos2d::CCDictionary<std::string, cocos2d::CCObject*> *cocosextDictionaryWithContentsOfFileThreadSafe(const char *pFileName);


/** Work with points */

static inline cocos2d::CCPoint  ccpMid ( const  cocos2d::CCPoint& v )
{
    return ccp ( v.x / 2.f, v.y / 2.f );
}

static inline  cocos2d::CCPoint  ccpMid ( const  cocos2d::CCSize& s )
{
    return ccp ( s.width / 2.f, s.height / 2.f );
}

/** Clamp angles */
extern cocos2d::CCPoint safeNormalize(const cocos2d::CCPoint v);
extern float clampAngleDeg(float angle);  
extern float clampAngleRad(float angle);    

/** Touch checker */
extern bool isNodeContainsTouch(cocos2d::CCNode& node, cocos2d::CCTouch* touch);

// Rect auxiliary functions

#define CGRectBottomLeft(rect) (rect.origin)
#define CGRectBottomRight(rect) ccp(rect.origin.x + rect.size.width, rect.origin.y)
#define CGRectTopRight(rect) ccp(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height)
#define CGRectTopLeft(rect) ccp(rect.origin.x, rect.origin.y + rect.size.height)
#define CGRectCenter(rect) ccp(rect.origin.x + rect.size.width / 2, rect.origin.y + rect.size.height / 2)
#define CGRectCenterTop(rect) ccp(rect.origin.x + rect.size.width / 2, rect.origin.y + rect.size.height)
#define CGRectCenterBottom(rect) ccp(rect.origin.x + rect.size.width / 2, rect.origin.y)
#define CGRectCenterLeft(rect) ccp(rect.origin.x, rect.origin.y + rect.size.height / 2)
#define CGRectCenterRight(rect) ccp(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height / 2)


/** Open site with url */
extern void openSiteWithURL(const char* url);

}; // end namespace cocosext

#endif // __CE_Cocos2dUtility_h__
